package com.jxt.myjxtmanager.ui.tableadapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.ClipboardManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.entity.StuClassShowBean;
import com.jxt.myjxtmanager.utils.ToastUtil;
import com.jxt.myjxtmanager.views.AutoAjustSizeTextView;

import java.util.List;

/**
 * Created by qm on 2017/3/6 0006.
 */
public class StuClassNexusAdapter extends BaseTableAdapter {
    private final NexusTypes familys[];
    private final String headers[] = {"班级编号", "班级", "班级负责人", "卫生情况", "纪律情况", "路队情况", "时间"};

    private Context mContext;
    private Activity mActivity;
    private Fragment fragment;
    //    private boolean searchFlag = false;

    private int header = 1;//1 16大项  2  7大项


    private final int[] widths = {
            100, 100, 100, 100, 150, 140, 120,
    };
    private final float density;
    private List<StuClassShowBean> workBeans;

    public StuClassNexusAdapter(Context context, Activity mActivity) {//Context context, Activity mActivity, NewDevicesPresenter presenter
        familys = new NexusTypes[]{
                new NexusTypes("Mobiles"),
        };

        density = context.getResources().getDisplayMetrics().density;
        this.mContext = context;
        this.mActivity = mActivity;
    }

    public void setDevicesDates(List<StuClassShowBean> workBeans) {
        this.workBeans = workBeans;
//            if (familys[0].list != null&&familys[0].list.size()>0){
//                familys[0].list = null;
//            }else{
//                familys[0].list = new ArrayList<>();
//            }
        if (NexusTypes.list != null) {
            NexusTypes.list.clear();
        }
        NexusTypes.list.addAll(workBeans);
    }

    @Override
    public int getRowCount() {
        return workBeans.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    private StuClassShowBean getData(int row) {
        int family = 0;
        while (row >= 0) {
//            row -= familys[family].size() + 1;
//            family++;
            row -= familys[0].size() + 1;
//            family++;
        }
//        family--;
//        return (DeviceInfo) familys[family].get(row + familys[family].size());
        return (StuClassShowBean) familys[0].get(row + familys[0].size());
    }

    @Override
    public View getView(int row, int column, View convertView, ViewGroup parent) {
        final View view;
        switch (getItemViewType(row, column)) {
            case 0:
                view = getFirstHeader(row, column, convertView, parent);
                break;
            case 1:
                view = getHeader(row, column, convertView, parent);
                break;
            case 2:
                view = getFirstBody(row, column, convertView, parent);
                break;
            case 3:
                view = getBody(row, column, convertView, parent);
                break;
            case 4:
                view = getFamilyView(row, column, convertView, parent);
                break;
            default:
                throw new RuntimeException("wtf?");
        }
        return view;
    }

    private View getFirstHeader(int row, int column, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_table_header_first, parent, false);
        }
        ((TextView) convertView.findViewById(android.R.id.text1)).setText(headers[0]);
        return convertView;
    }

    private View getHeader(int row, final int column, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_table_header, parent, false);
        }
        ((TextView) convertView.findViewById(android.R.id.text1)).setText(headers[column + 1]);

        final View finalConvertView = convertView;

        return convertView;
    }

    private View getFirstBody(final int row, int column, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_table_first, parent, false);
        }
        convertView.findViewById(R.id.text1).setBackgroundResource(row % 2 == 0 ? R.drawable.devices_list_item_bg1 : R.drawable.devices_list_item_bg3);
        ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getClass_Id());
//            setRestOpBg(convertView.findViewById(R.id.text1), row, getDevice(row));//设置休运不同级别背景颜色

        return convertView;
    }

    private View getBody(final int row, final int column, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_table, parent, false);
        }
        convertView.findViewById(R.id.text1).setBackgroundResource(row % 2 == 0 ? R.drawable.devices_list_item_bg1 : R.drawable.devices_list_item_bg3);

//            setRestOpBg(convertView.findViewById(R.id.text1), row, getDevice(row));//设置休运不同级别背景颜色
        if (column == 0) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getClassName());
        } else if (column == 1) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getClassLeader());
        } else if (column == 2) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getHealth());
        } else if (column == 3) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getDiscipline());
        } else if (column == 4) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getRoadTeam());
        } else if (column == 5) {
            ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setText(getData(row).getUpdateTime());
        }

        return convertView;
    }

    private void CanBeCope(final View convertView) {
        ((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(((AutoAjustSizeTextView) convertView.findViewById(R.id.text1)).getText());
                ToastUtil.showToast(mContext, "已复制");
                return false;
            }
        });
    }

    private View getFamilyView(int row, int column, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mActivity.getLayoutInflater().inflate(R.layout.item_table_family, parent, false);
        }
        convertView.setVisibility(View.GONE);
        final String string;
        if (column == -1) {
            string = getFamily(row).name;
        } else {
            string = "";
        }
        ((TextView) convertView.findViewById(android.R.id.text1)).setText(string);
        return convertView;
    }

    @Override
    public int getWidth(int column) {
        return Math.round(widths[column + 1] * density);
    }

    @Override
    public int getHeight(int row) {
        final int height;
        if (row == -1) {
            height = 35;
        } else if (isFamily(row)) {
            height = 0;
        } else {
            height = 45;
        }
        return Math.round(height * density);
    }

    @Override
    public int getItemViewType(int row, int column) {
        final int itemViewType;
        if (row == -1 && column == -1) {
            itemViewType = 0;
        } else if (row == -1) {
            itemViewType = 1;
        } else if (isFamily(row)) {
            itemViewType = 4;
        } else if (column == -1) {
            itemViewType = 2;
        } else {
            itemViewType = 3;
        }
        return itemViewType;
    }

    private boolean isFamily(int row) {
        int family = 0;
        while (row > 0) {
//            row -= familys[family].size() + 1;
//            family++;
            row -= familys[0].size() + 1;
//            family++;
        }
        return row == 0;
    }

    private NexusTypes getFamily(int row) {
        int family = 0;
        while (row >= 0) {
//            row -= familys[family].size() + 1;
//            family++;
            row -= familys[0].size() + 1;
//            family++;
        }
//        return familys[family - 1];
        return familys[0];
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

}